﻿using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;

public class Player : MonoBehaviour
{
    TextMeshProUGUI tmp;
    Table tb;
    public int money = 100, rank = 1, No_;
    public Card card0, card1, card2, card3, card4;
    public string todoT = "";
    public bool show = false;
    public bool dead = false;

    private void Awake()
    {
        tmp = GetComponent<TextMeshProUGUI>();
        tb = GameObject.Find("中").GetComponent<Table>();
    }

    public void Update_()
    {
        Show();
    }

    void Show()
    {
        string cardT = "";
        cardT += card0 is not null ? (show ? card0.ToString() : "?? ") : "-- ";
        cardT += card1 is not null ? card1.ToString() : "-- ";
        cardT += card2 is not null ? card2.ToString() : "-- ";
        cardT += card3 is not null ? card3.ToString() : "-- ";
        cardT += card4 is not null ? card4.ToString() : "-- ";
        tmp.text = "筹码：" + money
            + "\n卡牌：" + cardT
            + "\n牌型：" + ShowLv()
            + "\n操作：" + todoT;
        if (rank == 0)
            tmp.fontStyle = FontStyles.Underline;
        else
            tmp.fontStyle = FontStyles.Normal;
        if (dead)
            tmp.text = "筹码：" + money;

    }

    public Card[] GetCards(bool all = false)
    {
        List<Card> cards = new();
        if (all && card0 is not null) cards.Add(card0);
        if (card1 is not null) cards.Add(card1);
        if (card2 is not null) cards.Add(card2);
        if (card3 is not null) cards.Add(card3);
        if (card4 is not null) cards.Add(card4);
        return cards.ToArray();
    }

    public void Choose()
    {
        if (money == 0)
        {
            todoT = "<color=\"red\">已梭哈";
            return;
        }
        int giveup = 4, yes = 6, add = 0, all = 0;
        if (rank == 0)
        {
            giveup = 0; yes = 7; add = 2; all = 1;
        }
        else if (rank == 4)
        {
            if (P.Lv(GetCards(true)) > P.Lv(GetCards()) + 2)
            {
                giveup = 1; yes = 3; add = 4; all = 2;
            }
            else
            {
                giveup = 6; yes = 2; add = 1; all = 1;
            }
        }
        else if (P.Lv(GetCards(true)) > P.Lv(GetCards()) + 2)
        {
            giveup = 1; yes = 5; add = 3; all = 1;
        }
        if (card4 is not null && rank != 0 && P.Lv(GetCards(true)) == P.Lv(GetCards()))
        { giveup = 10; yes = 1; add = 1; all = 1; }
        int todo = Todo(giveup, yes, add, all);
        string str = "";
        switch (todo)
        {
            case 0:
                str = "放弃";
                dead = true;
                break;
            case 1:
                str = "<color=\"green\">跟注";
                int willGive = 0;
                if (money > 0) tb.LessMoney(No_);
                foreach (int m in tb.money)
                    if (m > willGive)
                        willGive = m;
                for (; tb.money[No_] < willGive && money > 0;)
                    tb.LessMoney(No_);
                break;
            case 2:
                str = "<color=\"blue\">加注";
                int willGive2 = 0;
                if (money > 0) tb.LessMoney(No_);
                foreach (int m in tb.money)
                    if (m > willGive2)
                        willGive2 = m;
                for (; tb.money[No_] < willGive2 + 10 && money > 0;)
                    tb.LessMoney(No_);
                break;
            case 3:
                str = "<color=\"red\">梭哈！";
                for (; money > 0;)
                    tb.LessMoney(No_);
                break;
        }
        todoT = todoT == str ? todoT + "*" : str;
    }

    int Todo(int giveup, int yes, int add, int all)
    {
        Debug.Log("AI意向" + giveup + yes + add + all);
        int lucky = Random.Range(0, giveup + yes + add + all) + 1;
        if (lucky <= giveup) return 0;
        lucky -= giveup;
        if (lucky <= yes) return 1;
        lucky -= yes;
        if (lucky <= add) return 2;
        return 3;
    }

    string ShowLv()
    {
        Rank rk = P.Lv(GetCards(show));
        switch (rk)
        {
            case Rank.ths:
                return "同花顺";
            case Rank.x4:
                return "炸弹";
            case Rank.x3a2:
                return "葫芦";
            case Rank.th:
                return "同花";
            case Rank.s:
                return "顺子";
            case Rank.x3:
                return "三条";
            case Rank.x2a2:
                return "两对";
            case Rank.x2:
                return "对子";
            case Rank.x1:
                return "散牌";
            default:
                return "";
        }
    }

    public void Giveup() => dead = true;
}
